package javacode;

import javacode.primary_class.Xml;
import javacode.primary_class.Excel;
import javacode.secondary_class.ColorRenderer;
import javacode.secondary_class.ExtensionFileFilter;
import javacode.secondary_class.ConvertString;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileFilter;
import nu.xom.ParsingException;
/** 
 * Lop nay la lop chinh, chua giao dien, xu li cac su kien
 * Goi lai cac lop da viet
 */
   
public class MainForm extends javax.swing.JFrame {
       
    String path_xml="";                                                         // chua duong dan file xml
    String filename_xml="";                                                     // chua ten file xml
    String path_excel="";                                                       // duong dan xuat file excel
    String filename_excel="";                                                   // ten file excel
    ConvertString workstring =new ConvertString();
    Excel excel = null;
    Xml xml = null;
    public MainForm() {
        initComponents();
    }
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btg_structure = new javax.swing.ButtonGroup();
        frm_aboutus = new javax.swing.JFrame();
        btn_close = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        txt_xml = new javax.swing.JTextField();
        txt_xls = new javax.swing.JTextField();
        btn_xml = new javax.swing.JLabel();
        btn_xls = new javax.swing.JLabel();
        btn_aboutus = new javax.swing.JLabel();
        btn_exit = new javax.swing.JLabel();
        btn_convert = new javax.swing.JLabel();
        cbx_fontcolor_hd = new javax.swing.JComboBox();
        cbx_bgrcolor_bd = new javax.swing.JComboBox();
        cbx_bgrcolor_hd = new javax.swing.JComboBox();
        cbx_fontcolor_bd = new javax.swing.JComboBox();
        rad_full = new javax.swing.JRadioButton();
        rad_simple = new javax.swing.JRadioButton();
        lb_nen = new javax.swing.JLabel();

        frm_aboutus.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        frm_aboutus.setTitle("About us");
        frm_aboutus.setAlwaysOnTop(true);
        frm_aboutus.setBackground(new java.awt.Color(255, 255, 255));
        frm_aboutus.setBounds(new java.awt.Rectangle(400, 200, 0, 0));
        frm_aboutus.setMinimumSize(new java.awt.Dimension(415, 340));
        frm_aboutus.setType(java.awt.Window.Type.POPUP);
        frm_aboutus.getContentPane().setLayout(null);

        btn_close.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/button_image/close.png"))); // NOI18N
        btn_close.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btn_closeMouseClicked(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                btn_closeMouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                btn_closeMouseExited(evt);
            }
        });
        frm_aboutus.getContentPane().add(btn_close);
        btn_close.setBounds(150, 260, 100, 30);

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/form_image/about.png"))); // NOI18N
        jLabel1.setText("jLabel1");
        frm_aboutus.getContentPane().add(jLabel1);
        jLabel1.setBounds(0, 0, 400, 300);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("TL1 Guide Conversion");
        setBounds(new java.awt.Rectangle(300, 100, 0, 0));
        setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        setMinimumSize(new java.awt.Dimension(606, 528));
        setName("main_frame"); // NOI18N
        setResizable(false);
        getContentPane().setLayout(null);

        txt_xml.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        txt_xml.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        txt_xml.setText("Muc thay doi thu 2");
        txt_xml.setToolTipText("Chọn nút File XML để thay đổi");
        txt_xml.setEnabled(false);
        txt_xml.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txt_xmlActionPerformed(evt);
            }
        });
        getContentPane().add(txt_xml);
        txt_xml.setBounds(30, 120, 450, 25);

        txt_xls.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        txt_xls.setForeground(new java.awt.Color(109, 109, 109));
        txt_xls.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        txt_xls.setText("Mục sau khi thay đổi");
        txt_xls.setToolTipText("Chọn nút File XLS để thay đổi");
        txt_xls.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txt_xlsActionPerformed(evt);
            }
        });
        getContentPane().add(txt_xls);
        txt_xls.setBounds(30, 170, 450, 25);

        btn_xml.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/button_image/xml.png"))); // NOI18N
        btn_xml.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btn_xmlMouseClicked(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                btn_xmlMouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                btn_xmlMouseExited(evt);
            }
        });
        getContentPane().add(btn_xml);
        btn_xml.setBounds(470, 110, 120, 40);

        btn_xls.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/button_image/xls.png"))); // NOI18N
        btn_xls.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btn_xlsMouseClicked(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                btn_xlsMouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                btn_xlsMouseExited(evt);
            }
        });
        getContentPane().add(btn_xls);
        btn_xls.setBounds(470, 160, 120, 40);

        btn_aboutus.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/button_image/about us.png"))); // NOI18N
        btn_aboutus.setText(" ");
        btn_aboutus.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btn_aboutusMouseClicked(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                btn_aboutusMouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                btn_aboutusMouseExited(evt);
            }
        });
        getContentPane().add(btn_aboutus);
        btn_aboutus.setBounds(250, 450, 120, 40);

        btn_exit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/button_image/exit.png"))); // NOI18N
        btn_exit.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btn_exitMouseClicked(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                btn_exitMouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                btn_exitMouseExited(evt);
            }
        });
        getContentPane().add(btn_exit);
        btn_exit.setBounds(430, 450, 130, 40);

        btn_convert.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/button_image/convert.png"))); // NOI18N
        btn_convert.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btn_convertMouseClicked(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                btn_convertMouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                btn_convertMouseExited(evt);
            }
        });
        getContentPane().add(btn_convert);
        btn_convert.setBounds(60, 450, 120, 40);

        Integer[] a = new Integer[8];
        for (int j = 0; j < a.length; j += 1)
        a[j] = new Integer(j);
        cbx_fontcolor_hd = new javax.swing.JComboBox(a);
        cbx_fontcolor_hd.setRenderer(new ColorRenderer());
        cbx_fontcolor_hd.setSelectedIndex(0);
        getContentPane().add(cbx_fontcolor_hd);
        cbx_fontcolor_hd.setBounds(230, 290, 60, 20);

        cbx_bgrcolor_bd = new javax.swing.JComboBox(a);
        cbx_bgrcolor_bd.setRenderer(new ColorRenderer());
        cbx_bgrcolor_bd.setSelectedIndex(0);
        getContentPane().add(cbx_bgrcolor_bd);
        cbx_bgrcolor_bd.setBounds(500, 320, 60, 20);

        cbx_bgrcolor_hd = new javax.swing.JComboBox(a);
        cbx_bgrcolor_hd.setRenderer(new ColorRenderer());
        cbx_bgrcolor_hd.setSelectedIndex(2);
        getContentPane().add(cbx_bgrcolor_hd);
        cbx_bgrcolor_hd.setBounds(230, 320, 60, 20);

        cbx_fontcolor_bd = new javax.swing.JComboBox(a);
        cbx_fontcolor_bd.setRenderer(new ColorRenderer());
        cbx_fontcolor_bd.setSelectedIndex(1);
        getContentPane().add(cbx_fontcolor_bd);
        cbx_fontcolor_bd.setBounds(500, 290, 60, 20);

        btg_structure.add(rad_full);
        rad_full.setOpaque(false);
        getContentPane().add(rad_full);
        rad_full.setBounds(230, 390, 21, 21);

        btg_structure.add(rad_simple);
        rad_simple.setSelected(true);
        rad_simple.setOpaque(false);
        getContentPane().add(rad_simple);
        rad_simple.setBounds(500, 390, 21, 21);

        lb_nen.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lb_nen.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/form_image/main_form.png"))); // NOI18N
        lb_nen.setIconTextGap(0);
        getContentPane().add(lb_nen);
        lb_nen.setBounds(0, 0, 600, 500);

        pack();
    }// </editor-fold>//GEN-END:initComponents


    private void btn_xmlMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btn_xmlMouseEntered
        btn_xml.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/button_image/xml-e.png")));
    }//GEN-LAST:event_btn_xmlMouseEntered

    private void btn_xmlMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btn_xmlMouseExited
       btn_xml.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/button_image/xml.png")));
    }//GEN-LAST:event_btn_xmlMouseExited

    private void btn_xlsMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btn_xlsMouseEntered
        btn_xls.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/button_image/xls-e.png")));
    }//GEN-LAST:event_btn_xlsMouseEntered

    private void btn_xlsMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btn_xlsMouseExited
        btn_xls.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/button_image/xls.png")));
    }//GEN-LAST:event_btn_xlsMouseExited

    private void btn_convertMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btn_convertMouseEntered
        btn_convert.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/button_image/convert-e.png")));
    }//GEN-LAST:event_btn_convertMouseEntered

    private void btn_convertMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btn_convertMouseExited
        btn_convert.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/button_image/convert.png")));
    }//GEN-LAST:event_btn_convertMouseExited

    private void btn_aboutusMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btn_aboutusMouseEntered
        btn_aboutus.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/button_image/about us-e.png")));
    }//GEN-LAST:event_btn_aboutusMouseEntered

    private void btn_aboutusMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btn_aboutusMouseExited
         btn_aboutus.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/button_image/about us.png")));
    }//GEN-LAST:event_btn_aboutusMouseExited

    private void btn_exitMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btn_exitMouseEntered
       btn_exit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/button_image/exit-e.png")));
    }//GEN-LAST:event_btn_exitMouseEntered

    private void btn_exitMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btn_exitMouseExited
        btn_exit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/button_image/exit.png")));
    }//GEN-LAST:event_btn_exitMouseExited

    private void btn_xmlMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btn_xmlMouseClicked
        JFileChooser file= new JFileChooser (".");                              // mo thu muc ngay cho file chay
         file.setDialogTitle("Chọn file XML cần chuyển");
         file.setAcceptAllFileFilterUsed(false);
         FileFilter xmltype = new ExtensionFileFilter("XML", new String[] { "XML"}); // loc file, chi cho lay file xml
         file.setFileFilter(xmltype);
         int status = file.showOpenDialog(null);
         if (status == JFileChooser.APPROVE_OPTION){
            String path= file.getSelectedFile().getPath();                      // lay path duong dan file xml duoc chon
             txt_xml.setText(path);
             path_xml= path;
             String filename= file.getSelectedFile().getName();                 // lay ten file xml
             filename_excel = workstring.getFileName(filename)+".xls";          // ten file excel = ten file xml bo phan mo rong + "xls"
             path = file.getSelectedFile().getParent()+"\\"+filename_excel;
             txt_xls.setText(path);        
 
         }         
    }//GEN-LAST:event_btn_xmlMouseClicked

    private void btn_xlsMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btn_xlsMouseClicked
        JFileChooser file= new JFileChooser (".");
         file.setDialogTitle("Chọn nơi lưu file excel");
         file.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);              // chi chon thu muc
         int status = file.showOpenDialog(null);
         if (status == JFileChooser.APPROVE_OPTION){
             String path= file.getSelectedFile().getPath();//+(char)92+filename_excel;
             char arpath[] =  path.toCharArray();
             if( arpath[arpath.length - 1] == 92)
                 path += filename_excel;
             else
                 path += ((char)92 + filename_excel);
             txt_xls.setText(path);     // ham relace de thay the chuoi \\ thanh \
             path_excel= path;
         } 
    }//GEN-LAST:event_btn_xlsMouseClicked

    private void btn_exitMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btn_exitMouseClicked
       System.exit(0);
    }//GEN-LAST:event_btn_exitMouseClicked

    private void btn_aboutusMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btn_aboutusMouseClicked
        frm_aboutus.setVisible(true);
    }//GEN-LAST:event_btn_aboutusMouseClicked

    private void btn_closeMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btn_closeMouseEntered
        btn_close.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/button_image/close-e.png")));
    }//GEN-LAST:event_btn_closeMouseEntered

    private void btn_closeMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btn_closeMouseExited
        btn_close.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/button_image/close.png")));
    }//GEN-LAST:event_btn_closeMouseExited

    private void btn_closeMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btn_closeMouseClicked
        frm_aboutus.setVisible(false);
    }//GEN-LAST:event_btn_closeMouseClicked

    private void btn_convertMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btn_convertMouseClicked
            path_excel = txt_xls.getText();
            int structure = rad_full.isSelected() ? 1 : 0;                          // neu chon cau truc file excel 14 cot(full) thi tra ve 1, nguoc lai tr ve 0;
            int text_color_hd = cbx_fontcolor_hd.getSelectedIndex();
            int bgr_color_hd = cbx_bgrcolor_hd.getSelectedIndex();
            int text_color_bd = cbx_fontcolor_bd.getSelectedIndex();
            int bgr_color_bd = cbx_bgrcolor_bd.getSelectedIndex();   
            if(workstring.checkPath(path_xml, path_excel)){
            try {
                excel = new Excel(structure);
                xml = new Xml(path_xml);
                if(structure > 0)
                    xml.convertAllToExcel(excel);
                else
                    xml.convertToExcel(excel);
                excel.formatExcel(bgr_color_hd, text_color_hd, bgr_color_bd, text_color_bd);
                filename_excel = workstring.getFileNameFull(path_excel);
                path_excel = path_excel.substring(0, path_excel.indexOf(filename_excel)); // chi lay dia chi den thu muc luu file excel
                excel.outputFile(filename_excel, path_excel);
                JOptionPane.showMessageDialog(rootPane, "Đã convert xong!");
            } catch (ParsingException ex) {
                Logger.getLogger(MainForm.class.getName()).log(Level.SEVERE, null, ex);
            }
            }else{
                txt_xls.setText("");
            }     
          
    }//GEN-LAST:event_btn_convertMouseClicked

    private void txt_xlsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txt_xlsActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txt_xlsActionPerformed

    private void txt_xmlActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txt_xmlActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txt_xmlActionPerformed
    
    /**
     * Ham main chua cac xu li chunh cua chuong trinh
     */
    public static void main(String args[]) {
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Windows".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(MainForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(MainForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(MainForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(MainForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                MainForm main_conver = new MainForm();
                main_conver.setAlwaysOnTop(false);
                main_conver.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup btg_structure;
    private javax.swing.JLabel btn_aboutus;
    private javax.swing.JLabel btn_close;
    private javax.swing.JLabel btn_convert;
    private javax.swing.JLabel btn_exit;
    private javax.swing.JLabel btn_xls;
    private javax.swing.JLabel btn_xml;
    private javax.swing.JComboBox cbx_bgrcolor_bd;
    private javax.swing.JComboBox cbx_bgrcolor_hd;
    private javax.swing.JComboBox cbx_fontcolor_bd;
    private javax.swing.JComboBox cbx_fontcolor_hd;
    private javax.swing.JFrame frm_aboutus;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel lb_nen;
    private javax.swing.JRadioButton rad_full;
    private javax.swing.JRadioButton rad_simple;
    private javax.swing.JTextField txt_xls;
    private javax.swing.JTextField txt_xml;
    // End of variables declaration//GEN-END:variables
}
